/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.jpdftools.feedback;

/**
 *
 * @author Mido
 */
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.logging.Level;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import project.jpdftools.managers.LoggerManager;

public class ZipFile {

    private String inputFile;    // file name and directory

    /**
     * Constructor with parameters
     *
     * @param inputFile
     */
    public ZipFile(String inputFile) {
        this.inputFile = inputFile;
    }

    /**
     * Zip a file and save it in the same directory
     */
    public void Zip() {
        try {
            final int BUFFER = 2048;
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(inputFile + ".zip");
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
            byte data[] = new byte[BUFFER];

            FileInputStream fi = new FileInputStream(inputFile + ".txt");
            origin = new BufferedInputStream(fi, BUFFER);
            ZipEntry entry = new ZipEntry(inputFile + ".txt");
            out.putNextEntry(entry);
            int count;
            while ((count = origin.read(data, 0,
                    BUFFER)) != -1) {
                out.write(data, 0, count);
            }
            // close streams
            origin.close();
            out.close();
            LoggerManager.getLogger().log(Level.INFO, "File compressed successfully. File name = {0}", inputFile + ".zip");
        } catch (Exception e) {
            LoggerManager.getLogger().log(Level.SEVERE, "File compression error. Message = {0}", project.jpdftools.toolkit.Toolkit.getStackTraceAsString(e.getCause()));
        }
    }
}
